<?php

/**
 *
 *
 * @package EDO
 * @subpackage Datatypes
 * @author Mikko Forsström
 *
 *
 */
abstract class Lead_EDO_Datatype_Base extends Lead_Event_Observable_Abstract 
{
	abstract function validate($validate);
	abstract function setValue($value);
	

	/**
	 * Field's name.
	 *
	 * @var string
	 */
	private $_name;

	/**
	 * Owner object
	 *
	 * @var EDO_object_base
	 */
	private $_owner;
	
	
	/**
	 * Config array is put here so that metadata can be stored there.
	 *
	 * @var array
	 */
	private $_config;
		

	protected $_SQLValueIsQuoted = false;
	protected $_SQLIsSortable = true;

	public function __construct(Lead_EDO_Object_Abstract $owner, $config = array())
	{
        $this->_owner = $owner;
        $this->_name = $config['name'];
        
        $this->_config = $config;
        
    }


	/**
	 * Returns owner
	 *
	 * @return Lead_EDO_Object_Abstract
	 */
	public function getOwner()
	{
	    return $this->_owner;
    }


    /**
     * Returns owner's class via it's getClass() method.
     *
     * @return string Dataobject's class
     */
    public function getOwnersClass()
    {
        return $this->getOwner()->getClass();
    }

	/**
	 * Returns field's name.
	 *
	 * @return string
	 */
	public function getName()
	{
	    return $this->_name;
	}

	/**
	 * Returns field's type. Lead_EDO_Datatype_ is removed from the beginning.
	 *
	 * @return unknown
	 */
	public function getType()
	{
		return substr(get_class($this), 9);
	}
	
	
	public function getConfig($key, $default = false)
	{
	    return (isset($this->_config[$key])) ? $this->_config[$key] : $default;
	}
	
	
	/* what are these? change must */


	public function addSQLForQuery(Query $query, SearchCondition $searchCond)
	{
		
	    
	    //$tables = $this->getOwner()->getTables();
		
		$theValue = $searchCond->getValue();
		if($this->SQLValueIsQuoted())
		{
			$theValue = "'{$theValue}'";
		}

		$tableName = $this->getOwner()->getFieldsTable($searchCond->getField());

		$table = $this->getOwner()->ldb->getTable($tableName);

		$query->getCurrentWhereGroup()->addWhereCond("{$tableName}.{$searchCond->getField()}", $searchCond->getOperator(), $theValue);

		$query->addJoin($table, "INNER", "edo.id = {$tableName}.id");


	}


	public function getSearchSQLForCondition(SearchCondition $searchCond)
	{

	    throw new Exception('this must change!');
	    
		// $tables = $this->getOwner()->getTables();
		$fields_tables = $this->getOwner()->getFieldsAndTables();

		$theValue = $searchCond->getValue();

		if(is_array($theValue))
		{
			$theValue = implode(", ", $theValue);
		}

		if($this->SQLValueIsQuoted())
		{
			$theValue = "'{$theValue}'";
		}

		$cond = "{$fields_tables[$searchCond->getField()]}.{$searchCond->getField()} {$searchCond->getOperator()} {$theValue}";
		if($searchCond->isNegated())
		{
			$cond = "NOT({$cond})";
		}

		die('juuri')	;
		
		return $cond;

	}


	public function SQLValueIsQuoted()
	{
		return $this->_SQLValueIsQuoted;
	}


	public function SQLIsSortable()
	{
		return $this->_SQLIsSortable;
	}



	public function getValueForSQL()
	{
	    return $this->getValue();
	}



}